const { User } = require('../model')
const { Result } = require('../util')
const md5 = require('md5')
// 登陆
exports.login = async (req, resp, next) => {
    try {
        req.body.password = md5(req.body.password)
        const result = await User.findOne(req.body)
        resp.json(Result.successObj(result))
    } catch (err) {
        // 跳转到错误处理中间件
        next(err)
    }
}

// 注册
exports.register = async (req, resp, next) => {
    try {
        req.body.password = md5(req.body.password)
        const result = await new User(req.body).save()
        console.log(result);
        resp.json(Result.success())
    } catch (err) {
        // 跳转到错误处理中间件
        next(err)
    }
}

// 删除用户
exports.deleteUser = async (req, resp, next) => {
    try {
        const { _id } = req.params
        const result = await User.deleteOne({ _id })
        resp.json(Result.successObj(result))
    } catch (err) {
        // 跳转到错误处理中间件
        next(err)
    }
}